//
//  XMViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMViewController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>


@implementation XMViewController

- (void)loadView {
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"FFFFFF"];
    [self setupBaseSubViews];
    self.navigationController.navigationBar.translucent = NO;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    // 要消除警告的代码
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
#pragma clang diagnostic pop
    self.navigationController.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    // 全局隐藏导航
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    if ([GKCover hasCover]) {
        [GKCover hideCover];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}

#pragma mark - setupSubviews
- (void)setupBaseSubViews {
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.offset(kNavBarAndStatusBarHeight);
    }];
    
    self.navTitleLa = self.navView.navTitleLa;
    self.navLineView = self.navView.navLineView;
    self.navLeftBtn = self.navView.navLeftBtn;
    self.navLeft2Btn = self.navView.navLeft2Btn;
    self.navRightBtn = self.navView.navRightBtn;
    self.navRight2Btn = self.navView.navRight2Btn;

    [self.navLeftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navLeft2Btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];

}

- (void)backAction {
    if (self.isFromPush) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)popToViewControlle:(NSString *)controllerName {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:NSClassFromString(controllerName)]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}


#pragma mark - 关于旋转的设置
//是否自动旋转:所有控制器默认不自动旋转，需要横屏的视图控制器中覆写此方法，返回YES
- (BOOL)shouldAutorotate {
    return NO;
}

//支持哪些屏幕方向:只支持竖屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

//默认方向:只支持正常竖屏
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
#pragma mark - lazy
- (XMNavView *)navView {
    if (_navView == nil) {
        _navView = [[XMNavView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
        _navView.backgroundColor = kNavBarBackGroundColor;
    }
    return _navView;
}

- (void)dealloc {
    NSLog(@"释放了%@", [self class]);
}

@end

#pragma mark - XMNavView

@implementation XMNavView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self addSubview:self.navTitleLa];
    [self addSubview:self.navLeftBtn];
    [self addSubview:self.navLeft2Btn];
    [self addSubview:self.navRightBtn];
    [self addSubview:self.navRight2Btn];
    [self addSubview:self.navLineView];
    
    [self.navTitleLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY)
            .offset((kNavBarAndStatusBarHeight - 44) / 2);
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    [self.navLeftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY)
            .offset((kNavBarAndStatusBarHeight - 44) / 2);
        make.left.equalTo(self.mas_left).offset(16);
        make.size.mas_equalTo(CGSizeMake(30, 44));
    }];
    
    [self.navLeft2Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY)
            .offset((kNavBarAndStatusBarHeight - 44) / 2);
        make.left.equalTo(self.navLeftBtn.mas_right).offset(0);
    }];
    
    [self.navRightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY)
            .offset((kNavBarAndStatusBarHeight - 44) / 2);
        make.right.equalTo(self.mas_right).offset(-16);
    }];
    
    [self.navRight2Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY)
            .offset((kNavBarAndStatusBarHeight - 44) / 2);
        make.right.equalTo(self.navRightBtn.mas_left).offset(-16);
    }];
    
    [self.navLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.offset(1);
    }];
}

- (UIView *)navLineView {
    if (_navLineView == nil) {
        _navLineView = [[UIView alloc] init];
        _navLineView.backgroundColor = [UIColor colorWithHexString:@"#EFEFEF"];
        _navLineView.hidden = YES;
    }
    return _navLineView;
}

- (UILabel *)navTitleLa {
    if (_navTitleLa == nil) {
        UILabel *la = [[UILabel alloc] init];
        la.font = [UIFont boldSystemFontOfSize:18];
        la.textColor = [UIColor blackColor];
        _navTitleLa = la;
    }
    return _navTitleLa;
}

- (UIButton *)navLeftBtn {
    if (_navLeftBtn == nil) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectZero];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"xm_arrow_back"] forState:UIControlStateNormal];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        btn.titleLabel.font = kUIFont(16);
        _navLeftBtn = btn;
    }
    return _navLeftBtn;
}

- (UIButton *)navLeft2Btn {
    if (_navLeft2Btn == nil) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectZero];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = kUIFontBold(16);
        _navLeft2Btn = btn;
    }
    return _navLeft2Btn;
}

- (UIButton *)navRightBtn {
    if (_navRightBtn == nil) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectZero];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = kUIFont(16);
        _navRightBtn = btn;
    }
    return _navRightBtn;
}

- (UIButton *)navRight2Btn {
    if (_navRight2Btn == nil) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectZero];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = kUIFont(16);
        _navRight2Btn = btn;
    }
    return _navRight2Btn;
}

@end
